package com.pointi.simGatewayM;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.pointi.emoTools.emoLicense;

public class ConfigurationData extends emoLicense
{
	// PCM_FILE
	public String s_pcmfile_con_type = "";
	public String s_pcmfile_con_protocol = "";
	public String s_pcmfile_con_address = "";
	public String s_pcmfile_con_port = "";
	public String s_pcmfile_con_accountid = "";
	public String s_pcmfile_con_accountpw = "";
	public String s_pcmfile_path = "";
	public String s_pcmfile_filechecksec = "";
	public String s_pcmfile_minplaysec = "";

	// GATEWAY
	public String s_gateway_address = "" ;
	public String s_gateway_port = "" ;
	public String s_thread_limit = "" ;
	public String s_file_limit = "" ;

	public ConfigurationData() { }
	
	public void InitializeData() 
	{
		// PCM_FILE
		s_pcmfile_con_type = "";
		s_pcmfile_con_protocol = "";
		s_pcmfile_con_address = "";
		s_pcmfile_con_port = "";
		s_pcmfile_con_accountid = "";
		s_pcmfile_con_accountpw = "";
		s_pcmfile_path = "";
		s_pcmfile_filechecksec = "120";
		s_pcmfile_minplaysec = "10";
		
		// GATEWAY
		s_gateway_address = "" ;
		s_gateway_port = "" ;
		s_thread_limit = "" ;
		s_file_limit = "" ;
		super.InitializeData() ;
	}
	
	public ConfigurationData ReadConfXML(String strFile) throws Exception 
	{
		NodeList mCATEGORY = null ;
		Node mNode = null ;
		ConfigurationData conf_data = new ConfigurationData() ;
		try
		{
			Document doc = super.ReadConfXML(strFile, conf_data) ;
			doc.getDocumentElement().normalize() ;
			if (doc.getDocumentElement().getNodeName().equals("configuration"))
			{
				// CONF | CATEGORY | PCM_FILE
				mCATEGORY = doc.getElementsByTagName("pcmfile") ;
				mNode = mCATEGORY.item(0) ;
				if (mNode.getNodeType() == Node.ELEMENT_NODE)
				{
					Element mElmnt = (Element) mNode ; // TYPE_CAST
					conf_data.s_pcmfile_con_type = fnReadTerminal(mElmnt, "type") ;
					conf_data.s_pcmfile_con_protocol = fnReadTerminal(mElmnt, "protocol") ;
					conf_data.s_pcmfile_con_address = fnReadTerminal(mElmnt, "address") ;
					conf_data.s_pcmfile_con_port = fnReadTerminal(mElmnt, "port") ;
					conf_data.s_pcmfile_con_accountid = fnReadTerminal(mElmnt, "accountid") ;
					conf_data.s_pcmfile_con_accountpw = fnReadTerminal(mElmnt, "accountpw") ;
					conf_data.s_pcmfile_path = fnReadTerminal(mElmnt, "path") ;
					conf_data.s_pcmfile_filechecksec = fnReadTerminal(mElmnt, "filecheckloopsec") ;
					conf_data.s_pcmfile_minplaysec = fnReadTerminal(mElmnt, "minplaysec") ;
				}
				
				// CONF | CATEGORY | GATEWAY
				mCATEGORY = doc.getElementsByTagName("gateway") ;
				mNode = mCATEGORY.item(0) ;
				if (mNode.getNodeType() == Node.ELEMENT_NODE)
				{
					Element mElmnt = (Element) mNode ; // TYPE_CAST
					conf_data.s_gateway_address = fnReadTerminal(mElmnt, "address") ;
					conf_data.s_gateway_port = fnReadTerminal(mElmnt, "port") ;
					conf_data.s_thread_limit = fnReadTerminal(mElmnt, "thread_limit") ;
					conf_data.s_file_limit = fnReadTerminal(mElmnt, "file_limit") ;
				}
			}
		} 
		catch (Exception e)  { e.printStackTrace() ; }		
		return conf_data;
	}
	
	public void WriteLogConfigurationData(ConfigurationData conf_data)
	{
		super.WriteLogConfigurationData(conf_data) ;
		System.out.println("------------------ pcmfile ------------------");
		System.out.println("type		: " + conf_data.s_pcmfile_con_type);
		System.out.println("protocol	: " + conf_data.s_pcmfile_con_protocol);
		System.out.println("address		: " + conf_data.s_pcmfile_con_address);
		System.out.println("port		: " + conf_data.s_pcmfile_con_port);
		System.out.println("accountid	: " + conf_data.s_pcmfile_con_accountid);
		System.out.println("accountpw	: " + conf_data.s_pcmfile_con_accountpw);
		System.out.println("path		: " + conf_data.s_pcmfile_path);
		System.out.println("filecheck	: " + conf_data.s_pcmfile_filechecksec);
		System.out.println("minplaysec	: " + conf_data.s_pcmfile_minplaysec);
		System.out.println("---------------------------------------------");
		System.out.println("------------------ gateway ------------------");
		System.out.println("serverip	: " + conf_data.s_gateway_address);
		System.out.println("serverport	: " + conf_data.s_gateway_port);
		System.out.println("threadlimit	: " + conf_data.s_thread_limit);
		System.out.println("filelimit	: " + conf_data.s_file_limit);
		System.out.println("---------------------------------------------");
	}
}
